import { nilV } from "../common";

function isFunc(val: any) {
    return typeof val === "function";
}
function isString(val: any) {
    return typeof val === "string";
}
// 是不是 数字
function isNum(val: any) {
    if (typeof val !== "number") {
        return false;
    }
    return !Number.isNaN(val);
}

/** 是不是  数组啊
 * @param {Object} val
 */
function isArray(val: any) {
    if (typeof Array.isArray === "function") {
        return Array.isArray(val);
    } else {
        return Object.prototype.toString.call(val) === "[object Array]";
    }
}

/**
 * 是否对象
 */
function isObject(val: any) {
    if (nilV(val)) {
        return false;
    }
    return Object.prototype.toString.call(val) === "[object Object]";
}

const Useful = {
    number(v: any, compare: number = 0) {
        if (!isNum(v)) {
            return false;
        }
        return v > compare;
    },
    string(v: any) {
        if (!isString(v)) {
            return false;
        }
        return v.length > 0;
    },
    array(v: any) {
        if (!isArray(v)) {
            return false;
        }
        return v.length > 0;
    },
    object(v: any) {
        if (!isObject(v)) {
            return false;
        }
        return Object.keys(v).length > 0;
    },
};

export { isArray, isObject, isNum, isFunc, isString, Useful };
